package com.jin.webvideo.util.base;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringUtil {
    public static final String EMPTY = "";
    public static String getDefault(String val, String defaultVal){
        if (isEmpty(val)){
            return defaultVal;
        }
        return val;
    }
    public static String substringTrim(String value, int start){
        value = value.trim();
        int len = value.length();
        int st = 0;
        char[] val = value.toCharArray();
        while(start > 0){
            st ++;
            while ( (st < len) && (val[st] <= ' ') ) {
                st++;
            }
            start--;
        }
        while(start < 0){
            len--;
            while ( (st < len) && (val[len - 1] <= ' ') ) {
                len--;
            }
            start ++;
        }

        return ((st > 0) || (len < value.length())) ? value.substring(st, len) : value.trim();
    }
    public static boolean isEmpty(String str){
        return null == str || str.isEmpty();
    }

    public static boolean isNumber(String number){
        boolean isNum = false;
        try {
            Long.valueOf(number);
            isNum = true;
        }
        catch (Exception e){ }
        return isNum;
    }
    public static int getNumber(String number, int defaultVal){
        try {
            return Integer.valueOf(number);
        }
        catch (Exception e){ }
        return defaultVal;
    }
    public static String join(char[] target, String separator, boolean bothEnds){
        StringBuilder builder = new StringBuilder(separator);
        for (char c : target){
            builder.append(c);
            builder.append(separator);
        }
        if (builder.length() == 0){
            return "";
        }
        if (bothEnds){
            return builder.toString();
        }
        return builder.substring(1, builder.length()-1);
    }

    public static String formatNumber(String number){
        number = number.trim();
        int index = number.indexOf(".");
        int length = number.length();
        int start = 0;
        int end = length;
        if ( index != -1){
            for (int i = length - 1; i > index; i --){
                if (number.charAt(i) != '0'){
                    end = i + 1;
                    break;
                }
            }
            length = index;
        }
        for (int i = 0, l = length; i < l; i++){
            if (number.charAt(i) != '0'){
                start = i;
                break;
            }
        }
        return number.substring(start, end);
    }
    public static boolean isJson(String val){
        if (isEmpty(val)){
            return false;
        }
        char first = val.charAt(0);
        char last = val.charAt(val.length()-1);
        if (first == '[' && last == ']' || first == '{' && last == '}'){
            return true;
        }
        return false;
    }
    public static boolean getBool(String val){
        if (!isEmpty(val)){
            if ("1".equals(val) || "true".equals(val)){
                return true;
            }
        }
        return false;
    }
    public static String eliminateWhiteSpace(String val) {
        StringBuilder builder = new StringBuilder();
        for (char item : val.toCharArray()) {
            if (item > ' ') {
                builder.append(item);
            }
        }
        return val.toString();
    }
    public static String[] split(String val, String regex){
        if (isEmpty(val)){
            return new String[0];
        }
        ArrayList<String> list = new ArrayList<>();
        int index = -1;
        while ( (index = val.indexOf(regex)) != -1){
            list.add(val.substring(0, index));
            val = val.substring(index + regex.length());
        }
        if (!val.isEmpty()){
            list.add(val);
        }
        return list.toArray(new String[0]);
    }
    public static String[] splitWithRegex(String val, String regex){
        String[] arr = {val, null};
        int index = val.indexOf(regex);
        if (index != -1){
            arr[0] = val.substring(0, index);
            arr[1] = "";
            if (index + regex.length() < val.length()){
                arr[1] = val.substring(index + regex.length());
            }
        }
        return arr;
    }
    public static String trim(String val){
        if (val == null){
            return null;
        }
        return val.trim();
    }

    public static boolean equals(String s1, String s2) {
        if (s1 == null || s2 == null) {
            if (s1 == s2) {
                return true;
            }
            return false;
        }
        return s1.equals(s2);
    }

    public static boolean match(String template, String val) {
        String[] parts = StringUtil.split(template, "*");
        List<String> list = Arrays.stream(parts).filter((part)-> !part.isEmpty()).toList();
        parts = new String[list.size()];
        list.toArray(parts);
        // 不以*结束，则结尾必须匹配
        if (!template.endsWith("*") && !val.endsWith(parts[parts.length-1])) {
            return false;
        }
        // 不以*开始，则开头必须匹配
        if (!template.startsWith("*") && !val.startsWith(parts[0])) {
            return false;
        }
        return match(val, 0, parts, 0);
    }

    private static boolean match(String val, int idx, String[] parts, int partIdx) {
        int index = idx;
        String part = parts[partIdx];
        if (partIdx == parts.length-1) {
            return val.indexOf(part, index) != -1;
        }
        while ( (index = val.indexOf(part, index)) != -1) {
            index += part.length();
            if (match(val, index, parts, partIdx+1)) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(String str, String c) {
        return str.indexOf(c) != -1;
    }
}
